import { login, logout, getInfo, getMenu } from '@/api/login'
import { getTokenName, setTokenName,getTokenValue, setTokenValue, removeToken, setId } from '@/utils/auth'
import { isHttp, isEmpty } from "@/utils/validate"
import defAva from '@/assets/images/profile.jpg'

const useUserStore = defineStore(
  'user',
  {
    state: () => ({
      tokenName: getTokenName(),
      tokenValue: getTokenValue(),
      id: '',
      name: '',
      avatar: '',
      roles: [],
      permissions: [],
      nickName: '',
    }),
    actions: {
      // 登录
      login(userInfo) {
        const account = userInfo.account.trim()
        const password = userInfo.password
        const loginType = userInfo.loginType
        const code = userInfo.code
        // const uuid = userInfo.uuid
        return new Promise((resolve, reject) => {
          login(account, password, loginType, code).then(res => {
            console.log('res', res) // 测试用，后面删除，打印re
            setTokenName('token')
            setTokenValue(res.data.token)
            localStorage.setItem('userInfo', JSON.stringify(res.data)) // 测试用，后面删除，打印re.dat
            this.tokenName = 'token'
            this.tokenValue = res.data.token
            // this.id = res.data.id
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 获取用户信息
      getInfo(id) {
        return new Promise((resolve, reject) => {
          const res = {
            "data": [
              {
                "id": 1,
                "kind": 1,
                "menuType": 2,
                "title": "首页",
                "menuKey": "index",
                "menuDesc": null,
                "menuUrl": null,
                "path": "/index",
                "menuIcon": null,
                "parentId": 0,
                "sortNum": 10,
                "codeNo": null,
                "status": 1,
                "isCharge": 0,
                "createTime": "2024-09-23 15:26:36",
                "createId": 1,
                "updateTime": "2024-09-23 15:26:43",
                "updateId": 1,
                "parent": null,
                "children": [],
                "parentName": null
              },
              {
                "id": 47,
                "kind": 1,
                "menuType": 1,
                "title": "会员管理",
                "menuKey": "member",
                "menuDesc": null,
                "menuUrl": null,
                "path": null,
                "menuIcon": "icon-huiyuanguanli",
                "parentId": 0,
                "sortNum": 10,
                "codeNo": null,
                "status": 1,
                "isCharge": 0,
                "createTime": "2025-05-04 00:44:31",
                "createId": 2,
                "updateTime": "2025-05-04 00:45:48",
                "updateId": 2,
                "parent": null,
                "children": [
                  {
                    "id": 48,
                    "kind": 1,
                    "menuType": 2,
                    "title": "会员列表",
                    "menuKey": "member",
                    "menuDesc": null,
                    "menuUrl": "/member/getList",
                    "path": "/members",
                    "menuIcon": null,
                    "parentId": 47,
                    "sortNum": 10,
                    "codeNo": null,
                    "status": 1,
                    "isCharge": 0,
                    "createTime": "2025-05-04 00:46:35",
                    "createId": 2,
                    "updateTime": "2025-05-25 22:00:09",
                    "updateId": 2,
                    "parent": null,
                    "children": null,
                    "parentName": null
                  }
                ],
                "parentName": null
              },
              {
                "id": 50,
                "kind": 1,
                "menuType": 1,
                "title": "合同管理",
                "menuKey": "sige",
                "menuDesc": null,
                "menuUrl": null,
                "path": null,
                "menuIcon": "icon-danjuguanli2",
                "parentId": 0,
                "sortNum": 30,
                "codeNo": null,
                "status": 1,
                "isCharge": 0,
                "createTime": "2025-05-04 00:49:27",
                "createId": 2,
                "updateTime": "2025-05-04 00:50:47",
                "updateId": 2,
                "parent": null,
                "children": [
                  {
                    "id": 52,
                    "kind": 1,
                    "menuType": 2,
                    "title": "合同列表",
                    "menuKey": "sige",
                    "menuDesc": null,
                    "menuUrl": "/sige/getList",
                    "path": "/contracts",
                    "menuIcon": "icon-caidanguanli4",
                    "parentId": 50,
                    "sortNum": 55,
                    "codeNo": null,
                    "status": 1,
                    "isCharge": 0,
                    "createTime": "2025-05-04 00:51:47",
                    "createId": 2,
                    "updateTime": "2025-05-25 22:00:37",
                    "updateId": 2,
                    "parent": null,
                    "children": null,
                    "parentName": null
                  }
                ],
                "parentName": null
              },
              {
                "id": 49,
                "kind": 1,
                "menuType": 1,
                "title": "单据管理",
                "menuKey": "bill",
                "menuDesc": null,
                "menuUrl": null,
                "path": null,
                "menuIcon": "icon-danjuguanli",
                "parentId": 0,
                "sortNum": 60,
                "codeNo": null,
                "status": 1,
                "isCharge": 0,
                "createTime": "2025-05-04 00:48:49",
                "createId": 2,
                "updateTime": "2025-05-26 00:12:47",
                "updateId": 2,
                "parent": null,
                "children": [
                  {
                    "id": 51,
                    "kind": 1,
                    "menuType": 2,
                    "title": "微信账单",
                    "menuKey": "wxBill",
                    "menuDesc": null,
                    "menuUrl": "/wxBill/getList",
                    "path": "/wxBill",
                    "menuIcon": "icon-caidanguanli5",
                    "parentId": 49,
                    "sortNum": 10,
                    "codeNo": null,
                    "status": 1,
                    "isCharge": 0,
                    "createTime": "2025-05-04 00:50:31",
                    "createId": 2,
                    "updateTime": "2025-05-25 22:00:52",
                    "updateId": 2,
                    "parent": null,
                    "children": null,
                    "parentName": null
                  }
                ],
                "parentName": null
              },
              {
                "id": 53,
                "kind": 1,
                "menuType": 1,
                "title": "投诉建议",
                "menuKey": null,
                "menuDesc": null,
                "menuUrl": null,
                "path": null,
                "menuIcon": "icon-taocanguanli",
                "parentId": 0,
                "sortNum": 60,
                "codeNo": null,
                "status": 1,
                "isCharge": 0,
                "createTime": "2025-05-26 00:14:21",
                "createId": 2,
                "updateTime": "2025-05-26 00:14:21",
                "updateId": 2,
                "parent": null,
                "children": [
                  {
                    "id": 54,
                    "kind": 1,
                    "menuType": 2,
                    "title": "投诉建议",
                    "menuKey": null,
                    "menuDesc": null,
                    "menuUrl": "/complaint/getList",
                    "path": "/complaints",
                    "menuIcon": null,
                    "parentId": 53,
                    "sortNum": 55,
                    "codeNo": null,
                    "status": 1,
                    "isCharge": 0,
                    "createTime": "2025-05-26 00:15:16",
                    "createId": 2,
                    "updateTime": "2025-05-26 00:15:16",
                    "updateId": 2,
                    "parent": null,
                    "children": null,
                    "parentName": null
                  }
                ],
                "parentName": null
              },
              {
                "id": 6,
                "kind": 1,
                "menuType": 1,
                "title": "组织架构",
                "menuKey": "renshi",
                "menuDesc": null,
                "menuUrl": null,
                "path": "",
                "menuIcon": "icon-bumenguanli1",
                "parentId": 0,
                "sortNum": 90,
                "codeNo": null,
                "status": 1,
                "isCharge": 0,
                "createTime": "2024-09-26 15:50:46",
                "createId": 1,
                "updateTime": "2025-05-26 00:12:28",
                "updateId": 2,
                "parent": null,
                "children": [
                  {
                    "id": 38,
                    "kind": 1,
                    "menuType": 2,
                    "title": "部门管理",
                    "menuKey": "depts",
                    "menuDesc": null,
                    "menuUrl": null,
                    "path": "/depts",
                    "menuIcon": null,
                    "parentId": 6,
                    "sortNum": 9,
                    "codeNo": null,
                    "status": 1,
                    "isCharge": 0,
                    "createTime": "2024-10-29 15:29:52",
                    "createId": 2,
                    "updateTime": "2025-05-02 23:58:27",
                    "updateId": 2,
                    "parent": null,
                    "children": null,
                    "parentName": null
                  },
                  {
                    "id": 7,
                    "kind": 1,
                    "menuType": 2,
                    "title": "员工列表",
                    "menuKey": "users",
                    "menuDesc": null,
                    "menuUrl": null,
                    "path": "/users",
                    "menuIcon": null,
                    "parentId": 6,
                    "sortNum": 20,
                    "codeNo": null,
                    "status": 1,
                    "isCharge": 0,
                    "createTime": "2024-09-26 16:18:37",
                    "createId": 1,
                    "updateTime": "2024-10-29 15:30:14",
                    "updateId": 2,
                    "parent": null,
                    "children": null,
                    "parentName": null
                  }
                ],
                "parentName": null
              },
              {
                "id": 2,
                "kind": 1,
                "menuType": 1,
                "title": "系统设置",
                "menuKey": "system",
                "menuDesc": null,
                "menuUrl": null,
                "path": "",
                "menuIcon": "icon-xitongshezhi",
                "parentId": 0,
                "sortNum": 99,
                "codeNo": null,
                "status": 1,
                "isCharge": 0,
                "parent": null,
                "children": [
                  {
                    "id": 4,
                    "kind": 1,
                    "menuType": 2,
                    "title": "角色管理",
                    "menuKey": "roles",
                    "menuDesc": null,
                    "menuUrl": null,
                    "path": "/roles",
                    "menuIcon": null,
                    "parentId": 2,
                    "sortNum": 10,
                    "codeNo": null,
                    "status": 1,
                    "isCharge": 0,
                    "createTime": "2024-09-24 13:54:56",
                    "createId": 1,
                    "updateTime": "2024-09-24 13:54:56",
                    "updateId": 1,
                    "parent": null,
                    "children": null,
                    "parentName": null
                  },
                  {
                    "id": 5,
                    "kind": 1,
                    "menuType": 2,
                    "title": "菜单管理",
                    "menuKey": "menus",
                    "menuDesc": null,
                    "menuUrl": null,
                    "path": "/menus",
                    "menuIcon": null,
                    "parentId": 2,
                    "sortNum": 20,
                    "codeNo": null,
                    "status": 1,
                    "isCharge": 0,
                    "createTime": "2024-09-24 13:54:56",
                    "createId": 1,
                    "updateTime": "2024-09-26 14:05:04",
                    "updateId": 1,
                    "parent": null,
                    "children": null,
                    "parentName": null
                  },
                  {
                    "id": 43,
                    "kind": 1,
                    "menuType": 2,
                    "title": "费用配置",
                    "menuKey": "sysCode",
                    "menuDesc": null,
                    "menuUrl": null,
                    "path": "/sysConfig",
                    "menuIcon": null,
                    "parentId": 2,
                    "sortNum": 55,
                    "codeNo": "60",
                    "status": 1,
                    "isCharge": 0,
                    "parent": null,
                    "children": null,
                    "parentName": null
                  }
                ],
                "parentName": null
              }
            ],
          }
          // getMenu().then(res => {
            this.roles = ['ROLE_DEFAULT']
            
                      resolve(res)
          // }).catch(error => {
          //   reject(error)
          // })
          // getInfo(id).then(res => {
          //   console.log('res', res) // 测试用，后面删除，打印re
          //   const user = res.data
          //   let avatar = user.avatar || ""
          //   if (!isHttp(avatar)) {
          //     avatar = (isEmpty(avatar)) ? defAva : import.meta.env.VITE_APP_BASE_API + avatar
          //   }
          //   if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
          //     this.roles = res.roles
          //     // this.permissions = res.permissions
          //   } else {
              // this.roles = ['ROLE_DEFAULT']
          //   }
          //   console.log(user)
          //   this.id = user.id
          //   this.name = user.name
          //   this.nickName = user.nickName
          //   this.avatar = avatar
          //   resolve(res)
          // }).catch(error => {
          //   reject(error)
          // })
        })
      },
      // 退出系统
      logOut() {
        return new Promise((resolve, reject) => {
          // logout(this.token).then(() => {
            this.token = ''
          //   this.roles = []
            this.permissions = []
            removeToken()
            resolve()
          // }).catch(error => {
          //   reject(error)
          // })
        })
      },
      setPermissions(permissions) {
        this.permissions = permissions
      }
    }
  })

export default useUserStore
